﻿@using Seal.Model;
@{
    MetaTable table = Model;

    //Parameters for this table
    table.Parameters.Add(new Parameter() { Name = MetaTable.ParameterNameMongoSync, BoolValue = true, DisplayName = "Generate Mongo DB Stages", Description = "If true, the elements and restrictions defined in the model are applied as stages in the 'Mongo DB Stages Script' of the table. If false, the 'Mongo DB Stages Script' can be manually overwritten." });
    table.Parameters.Add(new Parameter() { Name = MetaTable.ParameterNameMongoRestrictionOperator, Value = "$and", DisplayName = "Restriction operator applied", Description = "Define the operator applied if several restrictions are defined for the table.", Enums = new string[] { "$and","$or"}, UseOnlyEnumValues = false });
    table.Parameters.Add(new Parameter() { Name = MetaTable.ParameterNameMongoDatabase, Value = "", DisplayName = "Database Name", Description = "The Database name of the Mongo Server." });
    table.Parameters.Add(new Parameter() { Name = MetaTable.ParameterNameMongoCollection, Value = "", DisplayName = "Mongo Collection Name", Description = "The collection name to load." });
    table.Parameters.Add(new Parameter() { Name = MetaTable.ParameterNameMongoArrayName, Value = "", DisplayName = "Array field name to unwind", Description = "If set, the table is built with the values of the array specified using an 'unwind' stage." });
    table.Parameters.Add(new Parameter() { Name = "mongo_field_names_ignore", TextValue = "", DisplayName = "Field names to ignore", Description = "Defines the fields to ignore in the table. One field name per line. If set to empty, no field is ignored. Adding a $project stage in the 'Definition Init Script' may achieve the same result." });

    table.DefinitionInitScript = @"@using MongoDB.Bson
@{
    MetaTable metaTable = Model;
    if (!metaTable.WithDataLoad) {
        //Add a stage to limit the number of records for the table definition 
        metaTable.MongoStages.Add(new BsonDocument(""$limit"" , 100));
    }

    var arrayName = metaTable.GetValue(MetaTable.ParameterNameMongoArrayName);
    if (!string.IsNullOrEmpty(arrayName)) {
        //Unwind stage for array
        metaTable.MongoStages.Add(new BsonDocument(""$unwind"" , ""$""+arrayName));
    }

    //Add Mongo stages (like $project) here to change the table definition and values (e.g. adding a year column from a date, converting text to numeric, etc.)
    /*
    metaTable.MongoStages.Add(
        new BsonDocument(
            ""$project"",
            new BsonDocument{
{""transactions.amount"", 1},
{""year"", new BsonDocument(""$year"",""$transactions.date"")},
{""price"", new BsonDocument(""$toDouble"",""$transactions.price"")},
    }));
    */    
};
";

    table.DefinitionScript = @"@using System.Data
@using MongoDB.Driver
@using MongoDB.Bson
@{
    MetaTable metaTable = Model;
    MongoClient client = new MongoClient(metaTable.Source.Connection.FullConnectionString);
    var dbName = metaTable.GetValue(MetaTable.ParameterNameMongoDatabase);
    var collectionName = metaTable.GetValue(MetaTable.ParameterNameMongoCollection);
    if (!client.ListDatabaseNames().ToList().Contains(dbName)) {
        throw new Exception(""Database name not found on the server"");
    }
    var database = client.GetDatabase(dbName);
    if (!database.ListCollectionNames().ToList().Contains(collectionName)) {
        throw new Exception(""Collection name not found in the database"");
    }
    var collection = database.GetCollection<BsonDocument>(collectionName);
    var fieldsToIgnore = metaTable.GetValue(""mongo_field_names_ignore"").Replace(""\r\n"",""\n"").Replace(""\r"",""\n"").Split('\n');
    metaTable.NoSQLTable = DataTableLoader.FromMongoDB(collection.Aggregate<BsonDocument>(metaTable.MongoStages).ToList(), fieldsToIgnore);
}";

    table.LoadScript = @"";
}
